import itertools


class Solution:
    def near_mine(self, board, x, y):
        mine_cnt = [1 if board[xi][yi] == 'M' else 0 for xi, yi in itertools.product(range(x-1, x+2), range(y-1, y+2)) if len(board)>xi>=0 and len(board[0])>yi>=0]
        return sum(mine_cnt)


    def updateBoard(self, board: 'List[List[str]]', click: 'List[int]') -> 'List[List[str]]':
        # print(click)
        x, y = click
        if board[x][y] == 'M':
            # game over
            board[x][y] = 'X'
        elif board[x][y] == 'E':
            mine_cnt = self.near_mine(board, x, y)
            if mine_cnt == 0:
                board[x][y] = 'B'
                [self.updateBoard(board, [xi, yi]) for xi, yi in itertools.product(range(x-1, x+2), range(y-1, y+2)) if len(board)>xi>=0 and len(board[0])>yi>=0]
            else:
                board[x][y] = str(mine_cnt)
        else: # 'B'
            pass
        return board


if __name__ == '__main__':
    s = Solution()
    board = [['E', 'E', 'E', 'E', 'E'],
    ['E', 'E', 'M', 'E', 'E'],
    ['E', 'E', 'E', 'E', 'E'],
    ['E', 'E', 'E', 'E', 'E']]
    click = [3, 0]
    print(s.updateBoard(board, click))
    print(s.updateBoard(['E'], [0,0]))
